<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { SchoolCheckStatus, School, SchoolDel } from "@/http/api";

export default {
  components: { outMain },
  name: "",
  data() {
    return {
      tableData: [],
      schoolArr: [],
      statusArr: [
        { label: "全部", value: "" },
        { label: "正常", value: "1" },
        { label: "禁用", value: "3" },
      ],
      queryData: {
        school_status: "",
        school_name: "",
        school_address: "",
        page: 1,
        per_page: 10,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    deleteFn(row) {
      ElMessageBox.confirm("确定删除选择的学校吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        SchoolDel({ school_id: row.school_id }).then((res) => {
          if (res.data.code == 1) {
            ElMessage({
              type: "success",
              message: "删除成功",
            });
            this.getData();
          }
        });
      });
    },
    addFn(id) {
      this.$router.push({
        path: "/schoolListAdd",
        query: { id: id ? this.queryData.page : undefined },
      });
    },
    detailFn(id) {
      this.$router.push({
        path: "/schoolListDetail",
        query: { id: id, page: this.queryData.page },
      });
    },
    questCheck(data) {
      SchoolCheckStatus(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "操作成功",
          });
          this.getData();
        }
      });
    },
    checkStatus(row, type) {
      let str = type == 1 ? "启用" : "禁用";
      ElMessageBox.confirm(`确定${str}选择的学校吗?`, "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let data = {
          school_id: row.school_id,
          status: type,
        };
        this.questCheck(data);
      });
    },
    serchFn() {
      this.queryData.page = 1;
      this.getData();
    },
    getData() {
      School(this.queryData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
  },
  mounted() {},
  async created() {
    this.queryData.page = Number(localStorage.getItem("boot_page")) || 1;

    this.schoolArr = await this.$getCate.getSchoolList();
    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>学校列表（{{ total }}）</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft">
          <btn value="新增" @ok="addFn"></btn>
        </div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.school_address"
                placeholder="请输入地址查询"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.school_name"
                filterable
                clearable
                @change="getData"
                placeholder="请输入学校名称"
              >
                <el-option
                  v-for="item in schoolArr"
                  :key="item.school_id"
                  :label="item.school_name"
                  :value="item.school_name"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                @change="getData"
                v-model="queryData.school_status"
                clearable
                placeholder="请选择账号状态"
              >
                <el-option
                  v-for="item in statusArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column
          prop="school_number"
          width="90"
          show-overflow-tooltip
          label="学校编号"
        />
        <el-table-column
          prop="school_name"
          width="180"
          show-overflow-tooltip
          label="学校名称"
        />
        <el-table-column
          prop="school_person"
          width="80"
          show-overflow-tooltip
          label="负责人"
        />
        <el-table-column
          prop="school_person_mobile"
          show-overflow-tooltip
          width="120"
          label="手机号"
        />
        <el-table-column width="80" show-overflow-tooltip label="学校状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag v-if="row.school_status == 1">启用中</el-tag>
              <el-tag type="danger" v-if="row.school_status == 2"
                >已禁用</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="school_address"
          show-overflow-tooltip
          label="学校地址"
        />
        <el-table-column
          prop="create_time"
          width="180"
          show-overflow-tooltip
          label="创建时间"
        />
        <el-table-column width="140" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div class="greenText ctrlText" @click="detailFn(row.school_id)">
                详情
              </div>
              <div class="blueText ctrlText" @click="addFn(row.school_id)">
                编辑
              </div>
              <div
                class="blueText ctrlText"
                @click="checkStatus(row, 1)"
                v-if="row.school_status == 2"
              >
                启用
              </div>
              <div
                class="redText ctrlText"
                @click="checkStatus(row, 2)"
                v-if="row.school_status == 1"
              >
                禁用
              </div>
              <div class="redText ctrlText" v-if="false" @click="deleteFn(row)">
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
